import 'package:flutter/material.dart';
import 'package:jingyuan/commmon/Config.dart';
import 'package:jingyuan/commmon/EventBus.dart';
import 'package:jingyuan/commmon/LocalStorage.dart';
import 'package:jingyuan/commmon/Utils.dart';
import 'package:jingyuan/pages/about/index.dart';
import 'package:jingyuan/pages/book/index.dart';
import 'package:jingyuan/pages/mainTabPage/index.dart';
import 'package:jingyuan/commmon/GlobalData.dart';
import 'package:oktoast/oktoast.dart';

void main() {
  runApp(OKToast(child: const MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '淨緣',
      onGenerateRoute: (settings) {
        //String? 表示name为可空类型
        final String? name = settings.name;
        //Function? 表示pageContentBuilder为可空类型
        final Function? pageContentBuilder = Config.routes[name];
        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route =
                MaterialPageRoute(builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
      },
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: '淨緣'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _TabItem {
  _TabItem({required this.title, required this.name, required this.widget});

  String title;
  String name;
  Widget widget;
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  var _tabsData = [];
  List<Widget> _tabContentViews = [];
  List<Widget> _tabTitles = [];
  TabController? _tabController;

  @override
  void initState() {
    super.initState();

    _tabsData.add(_TabItem(
        title: GlobalData.autioTabName,
        name: GlobalData.autioTabName,
        widget: const MainTabPage()));

    _tabsData.add(_TabItem(title: "經書", name: "經書", widget: BookTabPage()));
    _tabsData.add(_TabItem(title: "小記", name: "小記", widget: const AboutTabPage()));

    _genTabContentViews();
    _tabController ??= TabController(length: _tabsData.length, vsync: this);

    setState(() {});

    FishEventBus.on<PlayEvent>().listen((event) {
      setState(() {
        for (var element in _tabsData) {
          if (GlobalData.currPlayingTab == element.name) {
            element.title = '▶ ' + element.name;
          } else {
            element.title = element.name;
          }
        }
      });
    });
  }

  //销毁时调用
  @override
  void dispose() {
    super.dispose();
    _tabController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: TabBar(
          tabs: _tabsData.map((e) {
            return Tab(text: e.title);
          }).toList(),
          isScrollable: true,
          padding: EdgeInsets.zero,
          controller: _tabController,
          tabAlignment: TabAlignment.center,
          onTap: (index) {},
        )),
        body: Column(children: [
          Expanded(
              child: TabBarView(
            controller: _tabController,
            children: _tabContentViews,
          )),
        ]));
  }

  _genTabContentViews() {
    _tabContentViews.clear();
    for (var page in _tabsData) {
      _tabContentViews.add(page.widget);
    }
  }
}
